<?php

namespace Standard\Bundle\ModuleAboutBundle\Services;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Doctrine\ORM\EntityManager;
use Symfony\Component\DependencyInjection\Container;

class BookSingleton {
    private $author = 'Gamma, Helm, Johnson, and Vlissides';
    private $title  = 'Design Patterns';
    private static $book = NULL;
    private static $isLoanedOut = FALSE;

    private function __construct() {
    }

    static function borrowBook() {
        if (FALSE == self::$isLoanedOut) {
            if (NULL == self::$book) {
               self::$book = new BookSingleton();
            }
            self::$isLoanedOut = TRUE;
            return self::$book;
        } else {
          return NULL;
        }
    }

    function returnBook(BookSingleton $bookReturned) {
        self::$isLoanedOut = FALSE;
    }

    function getAuthor() {return $this->author;}

    function getTitle() {return $this->title;}

    function getAuthorAndTitle() {
      return $this->getTitle() . ' by ' . $this->getAuthor();
    }
  }
 
class BookBorrower {
    private $borrowedBook;
    private $haveBook = FALSE;

    function __construct() {
    }

    function getAuthorAndTitle() {
      if (TRUE == $this->haveBook) {
        return $this->borrowedBook->getAuthorAndTitle();
      } else {
        return "I don't have the book";
      }
    }

    function borrowBook() {
      $this->borrowedBook = BookSingleton::borrowBook();
      if ($this->borrowedBook == NULL) {
        $this->haveBook = FALSE;
      } else {
        $this->haveBook = TRUE;
      }
    }

    function returnBook() {
      $this->borrowedBook->returnBook($this->borrowedBook);
    }
  }

class SingletonDesginPatten {
   //put your code here
    private $templating;
	
    private $router;

    private $em;

    private $container;

    public function __construct(EngineInterface $templating, 
                                UrlGeneratorInterface $router,
                                EntityManager $em, Container $container )
    {
        $this->templating = $templating;
        $this->router = $router;
        $this->em = $em;
        $this->container = $container;

    }
    
    function testDesginPatten(){
        $this->writeln('BEGIN TESTING SINGLETON PATTERN');
        $this->writeln('');

        $bookBorrower1 = new BookBorrower();
        $bookBorrower2 = new BookBorrower();

        $bookBorrower1->borrowBook();
        $this->writeln('BookBorrower1 asked to borrow the book');
        $this->writeln('BookBorrower1 Author and Title: ');
        $this->writeln($bookBorrower1->getAuthorAndTitle());
        $this->writeln('');

        $bookBorrower2->borrowBook();
        $this->writeln('BookBorrower2 asked to borrow the book');
        $this->writeln('BookBorrower2 Author and Title: ');
        $this->writeln($bookBorrower2->getAuthorAndTitle());
        $this->writeln('');

        $bookBorrower1->returnBook();
        $this->writeln('BookBorrower1 returned the book');
        $this->writeln('');

        $bookBorrower2->borrowBook();
        $this->writeln('BookBorrower2 Author and Title: ');
        $this->writeln($bookBorrower1->getAuthorAndTitle());
        $this->writeln('');

        $this->writeln('END TESTING SINGLETON PATTERN');

    }
    
    

    function writeln($line_in) {
        echo $line_in."<br/>";
    }
}
